<?php 
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Model_agent extends Model
{

  function __construct()
  {
    $this->Model_agent();
  }
  
  function Model_agent()
  {
    parent::Model();
  }

  function get_template_via_uuid($host_list_uuid) {
    log_message('debug', "model_agent:get_template_via_uuid function starting");
    $dbr = $this->load->database('read', TRUE);
    $sql0 = "select t1.id as host_list_id, t1.host_list_hostname, t1.host_list_uuid, t1.template_list_id as host_template_list_id, t2.id as template_list_id, t2.template_list_name, t2.template_list_desc, t3.template_list_id as map_template_list_id, t3.command_objects_id as map_command_objects_id, t4.id as command_id, t4.command_objects_name, t4.command_objects_command, t4.command_objects_desc from host_list as t1, template_list as t2, template_command_objects_map as t3, command_objects as t4 WHERE t1.template_list_id = t2.id AND t1.template_list_id = t3.template_list_id AND t3.command_objects_id = t4.id AND t1.host_list_uuid = '$host_list_uuid';";
    log_message('debug', "$sql0");
    qstart();
    $query = $dbr->query($sql0);
    qend();
    if($query->num_rows() > 0) {      
      return $query->result_array();
    }
    else {
      $data=array(array( "host_list_id" => "NULL",
			 "host_list_hostname:" => "NULL",
			 "host_list_uuid" => "$host_list_uuid",
			 "host_template_list_id" => "NULL",
			 "template_list_id" => "NULL",
			 "template_list_name" => "NULL",
			 "template_list_desc" => "NULL",
			 "map_template_list_id" => "NULL",
			 "map_command_objects_id" => "NULL",
			 "command_id" => "NULL",
			 "command_objects_name" => "NULL",
			 "command_objects_command" => "NULL"));
      return $data;
    }
  }

  function get_access_report() {
    log_message('debug', "model_hosts::get_data function starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select t1.*, t2.* from host_list as t1, access_report as t2 WHERE t1.host_list_uuid = t2.host_list_uuid order by creation_time,host_list_hostname;";
    $query = $dbr->query($sql);
    if($query->num_rows() > 0) {
      log_message('debug', "get_access_report:: returning data");
      return $query->result_array();
    }
    else {
      $data=array(array( "id" => "0",
			 "host_list_hostname" => "NULL",
			 "host_list_ipaddress" => "NULL",
			 "host_list_uuid" => "NULL",
			 "template_list_id" => "NULL",
			 "id" => "NULL",
			 "host_list_uuid" => "NULL",
			 "creation_time" => "NULL"));
      return $data;
    }
  }

  function log_request_uuid($host_list_uuid) {
    log_message('debug', "model_agent:log_request_uuid starting");
    $host_list_uuid = $this->db->escape_str($host_list_uuid);
    
    $dbr = $this->load->database('write', TRUE);
    $sql0="INSERT INTO `access_report` ( 
`id` ,
`host_list_uuid` ,
`creation_time`
)
VALUES (
NULL , '$host_list_uuid', NOW());";
    log_message('debug', "model_agent:log_request_uuid:: $sql0");
    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      log_message('debug', "model_agent:log_request_uuid:: Transaction failed.");
      $this->db->trans_off();
      return 1;
    } 
    else {
      $this->db->trans_off();
      return 0;
    }
  }


}
?>